package com.example.questionexam.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.questionexam.common.BaseResponse;
import com.example.questionexam.common.DeleteRequest;
import com.example.questionexam.common.ErrorCode;
import com.example.questionexam.common.ResultUtils;
import com.example.questionexam.exception.BusinessException;
import com.example.questionexam.exception.ThrowUtils;
import com.example.questionexam.model.dto.question.QuestionAddRequest;
import com.example.questionexam.model.dto.question.QuestionQueryRequest;
import com.example.questionexam.model.dto.question.QuestionUpdateRequest;
import com.example.questionexam.model.entity.Question;
import com.example.questionexam.model.vo.QuestionVO;
import com.example.questionexam.service.QuestionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author Maobohe
 * @createData 2024/5/27 19:34
 */
@RestController
@RequestMapping("/question")
@Slf4j
public class QuestionController {
    
    @Resource
    private QuestionService questionService;

    /**
     * 创建题目
     *
     * @param questionAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addQuestion(@RequestBody QuestionAddRequest questionAddRequest, HttpServletRequest request) {
        if (questionAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Question question = new Question();
        BeanUtils.copyProperties(questionAddRequest, question);
        // 校验
        questionService.validQuestion(question, true);
        boolean result = questionService.save(question);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(question.getId());
    }

    /**
     * 删除题目
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteQuestion(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = questionService.removeQuestion(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 更新题目
     *
     * @param questionUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateQuestion(@RequestBody QuestionUpdateRequest questionUpdateRequest,
                                            HttpServletRequest request) {
        if (questionUpdateRequest == null || questionUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Question question = new Question();
        BeanUtils.copyProperties(questionUpdateRequest, question);
        // 校验
        questionService.validQuestion(question, false);
        Question oldQuestion = questionService.getById(question.getId());
        ThrowUtils.throwIf(oldQuestion == null, ErrorCode.NOT_FOUND_ERROR);
        boolean result = questionService.updateById(question);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 根据 id 获取题目（带答案）
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<QuestionVO> getQuestionWithAnswer(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QuestionVO questionVO = questionService.getQuestionWithAnswer(id);
        ThrowUtils.throwIf(questionVO == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(questionVO);
    }

    /**
     * 分页获取用户列表
     *
     * @param questionQueryRequest
     * @param request
     * @return
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<Question>> listQuestionByPage(QuestionQueryRequest questionQueryRequest,
                                                   HttpServletRequest request) {
        long current = questionQueryRequest.getCurrent();
        long size = questionQueryRequest.getPageSize();
        Page<Question> questionPage = questionService.page(new Page<>(current, size),
                questionService.getQueryWrapper(questionQueryRequest));
        return ResultUtils.success(questionPage);
    }
}
